package com.sedshop.shop.library.volley;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.sedshop.shop.library.constant.AppConfig;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/5/19.
 */
public class MyVolleyRequest {
    public static final String APPID_BASE = "553083cb2fba6641a8b92543";
    public static final String APPKEY_BASE_ = "dhdgeyqifgswuyetgdtwiwue";

    public static final String APPID_RELEASE = "55e682356a031158e9652b43";
    public static final String APPKEY_RELEASE = "aorbgeqcbvleoqyrpkpahgch";

    public static final String APPID = AppConfig.DEBUG  ? APPID_BASE:APPID_RELEASE;
    public static final String APPKEY = AppConfig.DEBUG  ? APPKEY_BASE_:APPKEY_RELEASE;

    private static final String TAG = "MyVolleyRequest";

    private Context context;

    public static String userAgent;

    public MyVolleyRequest(Context context) {
        this.context = context;
    }

    public void initUserAgent(){
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context
                    .getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String versionName = packInfo.versionName;
        String appName = "falvSDK";

        userAgent = "XJBrowser/" + appName + "-" + versionName;
    }

    public void sendRequest(int method, String url, JSONObject jsonRequest, final OnResponseListener listener,final OnErrorListener errorListener) {
        initUserAgent();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, jsonRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                listener.onResponse(response);
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                errorListener.onErrorResponse(error);
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                //Map<String, String> defaultHeaders = super.getHeaders();
                HashMap<String, String> headers = new HashMap<String,String>();
                headers.put("Content-Type", "application/json");
                headers.put("x-xjkj-app-id", APPID);
                String timestamp = String.valueOf(System.currentTimeMillis());
                String appSign = MD5Utils.getMD5Str(timestamp + APPKEY)+","+ timestamp;
                LogUtils.d(TAG, "appSign = " + appSign);
                headers.put("x-xjkj-app-sign",appSign);
                //x-xjkj-no-rest:true
                headers.put("x-xjkj-no-rest","true");
                //LogUtils.d(TAG, "defaultHeaders = " + defaultHeaders.get("User-Agent"));
                LogUtils.d(TAG, "userAgent = " + userAgent);
                headers.put("User-Agent",userAgent);
                return headers;
            }

        };

        Volley.newRequestQueue(context).add(jsonObjectRequest);
    }

    public void sendRequest(int method, String url, JSONObject jsonRequest, final String sessionToken, final OnResponseListener listener,final OnErrorListener errorListener) {
        initUserAgent();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(method, url, jsonRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                listener.onResponse(response);
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                errorListener.onErrorResponse(error);
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String,String>();
                headers.put("Content-Type", "application/json");
                headers.put("x-xjkj-app-id", APPID);
                String timestamp = String.valueOf(System.currentTimeMillis());
                String appSign = MD5Utils.getMD5Str(timestamp + APPKEY)+","+ timestamp;
                headers.put("x-xjkj-app-sign",appSign);
                headers.put("x-xjkj-session-token",sessionToken);
                //x-xjkj-no-rest:true
                headers.put("x-xjkj-no-rest","true");
                LogUtils.d(TAG, "userAgent = " + userAgent);
                headers.put("User-Agent",userAgent);
                return headers;
            }
        };

        Volley.newRequestQueue(context).add(jsonObjectRequest);
    }

    public interface OnResponseListener{
        boolean onResponse(JSONObject response);
    }

    public interface OnErrorListener{
        void onErrorResponse(VolleyError error);
    }
}
